// pages/mine/address.js
Page({

  
  data: {
      flag1:false,
      addresslist:[],disabled:false
  },

  
  onShow: function (options) {
       this.getaddresslist()

  },
  //获取地址列表
  getaddresslist(){
    const uid=wx.getStorageSync('uid')
      wx.request({
        url: 'https://zujieba.com/api/address/mine_address.html',
        data:{uid},
        method:"POST",
        success:(res)=>{
          console.log(res);
          
          const addresslist=res.data.data
          this.setData({
            addresslist
          })
           

        }
      })
  },
  //切换图片
  toggleclick1(e){


    const currentindex=e.currentTarget.dataset.index
    console.log(currentindex);
     const addresslist=this.data.addresslist
     
     const address=addresslist.findIndex((item)=>{
         return item.type==1
     })
     
     
     const address1=addresslist.findIndex((item,index)=>{
         return index==currentindex
     })
      if(address!=-1){addresslist[address].type=2}
     addresslist[address1].type=1
     this.setData({
       addresslist
     })
     const uid=wx.getStorageSync('uid')
     const id=this.data.addresslist[currentindex].id
     const type=2
     const params={uid,id,type}
     wx.request({
       url: 'https://zujieba.com/api/address/default.html',
       data:params,
       success:(res)=>{
         console.log(res);
         

       }
     })   
        

  },
  //点击删除按钮
  removeclick(e){
    console.log(e);
    
   wx.showModal({
     content:"确认删除该地址吗",
     showCancel:true,
     complete:(res)=>{
        if(res.confirm==true){
          const uid=wx.getStorageSync('uid')
          const id=e.currentTarget.dataset.id
          const params={uid,id}
          console.log(params);
          
          wx.request({
            url: 'https://zujieba.com/api/address/del_address.html',
            data:params,
            success:(res)=>{
              console.log(res);
              if(res.success==1){
                wx.showToast({
                  title: res.data.tips,
                  icon:"success",
                  duration:2000
                  
                })
              }else{
                wx.showToast({
                  title: res.data.tips,
                  icon:'success',
                  duration:2000

                })
              }
             
             this. getaddresslist()
              
            },
            fail(){
              wx.showToast({
                title: '删除失败',
                icon:'loading',
                duration:2000
              })
            }
          })
        }
        
     }
   })
  },
  

  
  // 添加地址
  address_save:function(){
    wx.navigateTo({
         url: '../../mine/address/address_save',
    })
  },

  // 编辑地址
  address_edit:function(e){
    const id=e.currentTarget.dataset.id
    //type是选中和未选中
    const type=e.currentTarget.dataset.type
    
    
    wx.navigateTo({
         url: "../../mine/address/address_edit?id="+id+"&type="+type
    })
  },
  
  
})